package com.example.dell.intelligentagriculture.utils;


import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

/**
 * 轮播图 adapter
 */
public class SlideAdapter extends PagerAdapter {
    private ImageView[] bandimgArray;

    public SlideAdapter(ImageView[] bandimgArray) {
        this.bandimgArray = bandimgArray;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    }
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        int i = position % 3;
        ImageView imageView = bandimgArray[i];
        //获取imageview的父容器
        ViewParent vp = imageView.getParent();
        if (vp != null) {
            ViewGroup viewGroup = (ViewGroup) vp;
            viewGroup.removeView(imageView);
        }
        container.addView(imageView);
        return imageView;
    }
}
